load packages

library(tidyverse)
library(knitr)

define variables

# paths
outputDir = '/Volumes/psych-cog/dsnlab/auto-motion-output/'

# variables
study = "tds"

load data

# global intensity file created using calculate_global_intensities.R
trash = read.csv(paste0(outputDir,study,'_autoTrash.csv'))

# manually coded file created using manually_coded.R
manual = read.csv(paste0(outputDir,study,'_manuallyCoded.csv'))

# afni 3dToutCount outlier created using merge_outcount.R
outcount = read.csv(paste0(outputDir,study,'_outcount.csv')) %>% 
  filter(poly == "p2") %>%
  mutate(trashOut = ifelse(outliers > .075, 1, 0))

compare to manual data

# filter trash dataframe and join with filteredMotion
joined = trash %>% 
  left_join(., manual, by = c("subjectID","run","volume")) %>%
  left_join(., outcount, by = c("subjectID","run","volume")) %>%
  select(subjectID, run, volume, volMean, volSD, trashDiff, trashOut, trash) %>%
  mutate(auto = ifelse(trashDiff == 1 & trash == 1, 2, 
                                ifelse(trashDiff == 1 & trash == 0, 3, trash)),
         outcount = ifelse(trashOut == 1 & trash == 1, 2, 
                                ifelse(trashOut == 1 & trash == 0, 3, trash))) %>%
  gather(compare, code, -c(subjectID, run, volume, volMean, volSD, trashDiff, trashOut, trash))

# check false negatives
falseNeg.auto = joined %>% filter(trashDiff == 0 & trash == 1)
falseNeg.outcount = joined %>% filter(trashOut == 0 & trash == 1)

# check false positives
falsePos.auto = joined %>% filter(trashDiff == 1 & trash == 0)
falsePos.outcount = joined %>% filter(trashOut == 1 & trash == 0)

# check hits
hits.auto = joined %>% filter(trashDiff == 1 & trash == 1)
hits.outcount = joined %>% filter(trashOut == 1 & trash == 1)

summarize results

summarize by participants

nVol = joined %>% group_by(subjectID) %>% summarize(nVol = length(volume))
summaryPos = falsePos.auto %>% group_by(subjectID) %>% summarize(falsePos = sum(trashDiff, na.rm=T))
summaryNeg = falseNeg.auto %>% group_by(subjectID) %>% summarize(falseNeg = sum(trash, na.rm=T))
summaryPosNeg = nVol %>%
  full_join(., summaryPos, by = "subjectID") %>% 
  full_join(., summaryNeg, by = "subjectID") %>% 
  mutate(falseNeg = ifelse(is.na(falseNeg), 0, falseNeg),
         falsePos = ifelse(is.na(falsePos), 0, falsePos),
         totalErrors = falsePos + falseNeg,
         percentErrors = (totalErrors/nVol)*100)